local guitu = fk.CreateSkill {
  name = "guitu",
}

guitu:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Start and
      #table.filter(player.room.alive_players, function(p) return p:getEquipment(Card.SubtypeWeapon) end) > 1
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(table.filter(room.alive_players, function(p)
      return #p:getEquipments(Card.SubtypeWeapon) > 0 end), function (p) return p.id end)
    local tos = room:askForChoosePlayers(player, targets, 2, 2, "#guitu-choose", self.name, true)
    if #tos == 2 then
      event:setCostData(self,tos)
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(event:getCostData(self), Util.Id2PlayerMapper)
    local n = {targets[1]:getAttackRange(), targets[2]:getAttackRange()}
    local cards = {}
    for _, p in ipairs(targets) do
      if #p:getEquipments(Card.SubtypeWeapon) == 1 then
        table.insert(cards, p:getEquipments(Card.SubtypeWeapon))
      else
        local card = U.askforChooseCardsAndChoice(player, p:getEquipments(Card.SubtypeWeapon), {"OK"}, self.name,
          "#guitu-card::"..p.id)
        table.insert(cards, card)
      end
    end
    U.swapCards(room, player, targets[1], targets[2], cards[1], cards[2], self.name, Card.PlayerEquip)
    for i = 1, 2, 1 do
      if not targets[i].dead and targets[i]:isWounded() and targets[i]:getAttackRange() < n[i] then
        room:recover{
          who = targets[i],
          num = 1,
          recoverBy = player,
          skillName = self.name,
        }
      end
    end
  end,
})

return guitu